package hk.m4s.lr.repair.test.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.GoodsSignModel;
import hk.m4s.lr.repair.test.model.GoodsStyleModel;
import hk.m4s.lr.repair.test.ui.goods.SlectGoodsSignActivity;
import hk.m4s.lr.repair.test.views.InnerGridView;


/**
 * Created by qi.wang on 2017/3/16.
 */

public class StyleGridsAdapter extends BaseAdapter{
    public List<GoodsSignModel.PropertyBean> list;
    SlectGoodsSignActivity context;
    LayoutInflater inflater;
    private SizeGridsAdapter sizeGridsAdapter;
    private String[] colorSize;
    public int flag=-1;
    public StyleGridsAdapter(SlectGoodsSignActivity context, List<GoodsSignModel.PropertyBean> data){
        this.context=context;
        this.list=data;
        inflater=LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if(convertView==null){
            holder=new ViewHolder();
            convertView=inflater.inflate(R.layout.list_item_goods_style,parent,false);
            holder.goods_name=(TextView)convertView.findViewById(R.id.styeName);
            holder.showGrid=(InnerGridView) convertView.findViewById(R.id.color_gird);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        holder.goods_name.setText(list.get(position).getTitle());
        String []size=list.get(position).getValue().split(",");
        List<GoodsStyleModel> sizeList = new ArrayList<>();
        for(int i=0;i<size.length;i++){
            GoodsStyleModel model=new GoodsStyleModel();
            model.setId(position+"");
            if(colorSize!=null) {
                for (int j = 0; j < colorSize.length; j++) {
                    if (size[i].equals(colorSize[j])) {
                        model.setSelect("1");
                    }
                }
            }
            model.setBrand_name(list.get(position).getNumber());
            model.setGoodsStyle(size[i]);
            sizeList.add(model);
            sizeGridsAdapter = new SizeGridsAdapter(context, sizeList);
            holder.showGrid.setAdapter(sizeGridsAdapter);
        }

        return convertView;
    }

    public static class ViewHolder{
        TextView goods_name;
        InnerGridView showGrid;
    }
    public void updateGird(String colors){
        colorSize=colors.split(",");
    }
}
